package org.lsb.app_back.controller;

import com.github.pagehelper.PageInfo;
import org.lsb.app_back.domain.AppInfo;
import org.lsb.app_back.service.AppInfoService;
import org.lsb.app_back.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/appInfo")
public class AppinfoController {
    @Autowired
    AppInfoService appInfoService;


    @Value("${upload.path}")
    String uploadPath;//上传路径
    @GetMapping("/test")
    public String test() {
        return "这是一个受保护的接口，需要有效的 JWT 令牌才能访问";
    }

    /**
     * 分页查询    默认1
     * @param appInfo
     * @param pageNum
     * @return
     */
    @PostMapping("/page")
    public ResultVo page(@RequestBody AppInfo appInfo ,@RequestParam(defaultValue = "1") Integer pageNum){
          //分页查询的依赖中的对象PageInfo，数据 List ,total,pages
          PageInfo pageInfo= appInfoService.getPage(appInfo,pageNum);
          return ResultVo.success("分页查询成功",pageInfo);

    }

    /**
     * 校验apkname不能重复
     * @return
     */
    @GetMapping("/validateApkName")
    public ResultVo validateApkName(@RequestParam(required = true)String apkname,@RequestParam(required = false) Long id){
        Boolean flag=  appInfoService.validateApkName(apkname,id);

        return  ResultVo.success("查apkname情况",flag);


    }

    /**
     * 新增列表
     * @param appInfo
     * @return
     */
    @PostMapping("/add")
    public ResultVo add(@RequestBody AppInfo appInfo){
        appInfoService.saveOrupdate(appInfo);
        return ResultVo.success("操作成功",null);

    }



    @PostMapping("/uplogo/{id}")
    public ResultVo upLogo(@PathVariable Long id, MultipartFile logo){

        if(logo!=null && !logo.isEmpty()){
            //文件原名
            String originalFilename = logo.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

           if(logo.getSize()>1024*1024){
               return ResultVo.error("文件太大，不能超过1M");
           }
           else if(     suffix.equalsIgnoreCase(".jpg")||
                    suffix.equalsIgnoreCase(".jpeg")||
                    suffix.equalsIgnoreCase(".gif")||
                    suffix.equalsIgnoreCase(".png"))
           {
                //上传文件
               File savePath=new File(uploadPath);
               if(!savePath.exists()){
                   savePath.mkdir();//目录如果不存在，则创建
               }

               String newfileName= UUID.randomUUID().toString().replace("-","");
               File saveFile =new File(uploadPath+newfileName+suffix);

               try {
                   logo.transferTo(saveFile);
                   //上传成功

                   //保存数据库
                AppInfo appInfo=   appInfoService.getById(id);
                appInfo.setLogopicpath(newfileName+suffix);
                appInfoService.saveOrupdate(appInfo);
               return ResultVo.success("上传logo成功,",null);


               } catch (IOException e) {
                   e.printStackTrace(); // 打印异常堆栈信息
                   throw new RuntimeException("文件上传过程异常");
               }


           }else {
                return ResultVo.error("文件格式不对");
            }

        }else{
            return ResultVo.error("文件必传");
        }


    }


}
